def p1():
    import pandas as pd
    import json

    # 定义输入文件路径和输出文件路径
    input_file = r"D:\家宽\2025-04\压缩包2025-03-12T21_07_34.569(1)\file\wlanNeighbor_daily_rolling.log.202503121816.14.csv"
    output_file = r"D:\家宽\2025-04\压缩包2025-03-12T21_07_34.569(1)\file\wlanNeighbor_daily_rolling.202503121816.14.csv"

    # 用于存储所有处理后的数据
    all_data = []

    try:
        # 打开输入文件进行读取
        with open(input_file, 'r', encoding='utf-8') as file:
            # 逐行读取文件
            for line in file:
                try:
                    # 解析当前行的 JSON 数据
                    data = json.loads(line)
                    # 提取 MAC 和 timestamp
                    mac = data["MAC"]
                    timestamp = data["timestamp"]
                    # 检查 data 字段是否存在
                    if "data" in data and "Radios" in data["data"]:
                        # 获取 Radios 列表
                        radios = data["data"]["Radios"]
                        # 在每个 Radios 条目添加 MAC 和 timestamp 字段
                        for radio in radios:
                            radio["MAC"] = mac
                            radio["timestamp"] = timestamp
                            # 将处理后的条目添加到 all_data 列表中
                            all_data.append(radio)
                    else:
                        print(f"该行缺少 data 或 Radios 字段: {line}")
                except json.JSONDecodeError:
                    print(f"无法解析行: {line}")
    except FileNotFoundError:
        print(f"错误: 文件 {input_file} 未找到。")
    except Exception as e:
        print(f"发生未知错误: {e}")

    # 如果 all_data 列表不为空
    if all_data:
        # 创建 DataFrame
        df = pd.DataFrame(all_data, columns=["MAC", "timestamp", "Radio", "SSID", "MacAddress", "Channel", "RSSI"])
        try:
            # 将 DataFrame 保存为 CSV 文件
            df = df.drop_duplicates()
            df.to_csv(output_file, index=False)
            print(f"数据已成功保存到 {output_file}")
        except Exception as e:
            print(f"保存 CSV 文件时出错: {e}")
    else:
        print("未找到有效的数据。")


def p2():
    import pandas as pd
    import json

    # 定义输入文件路径和输出文件路径
    input_file = r"D:\家宽\2025-04\压缩包2025-03-12T21_07_34.569(1)\file\uplinkStatus_daily_rolling.csv"
    output_file = r"D:\家宽\2025-04\压缩包2025-03-12T21_07_34.569(1)\file\uplinkStatus.csv"

    # 用于存储所有处理后的数据
    all_data = []

    try:
        # 打开输入文件进行读取
        with open(input_file, 'r', encoding='utf-8') as file:
            # 逐行读取文件
            for line in file:
                try:
                    line = line[14:]
                    # 解析当前行的 JSON 数据
                    data = json.loads(line)
                    # 提取 MAC 和 timestamp
                    mac = data["MAC"]
                    timestamp = data["timestamp"]
                    # 检查 data 字段是否存在
                    res = data['data']
                    res["MAC"] = mac
                    res["timestamp"] = timestamp
                    all_data.append(res)

                except json.JSONDecodeError:
                    print(f"无法解析行: {line}")
    except FileNotFoundError:
        print(f"错误: 文件 {input_file} 未找到。")
    except Exception as e:
        print(f"发生未知错误: {e}")

    # 如果 all_data 列表不为空
    if all_data:
        # 创建 DataFrame
        df = pd.DataFrame(all_data)
        try:
            # 将 DataFrame 保存为 CSV 文件
            print(df)
            # df = df.drop_duplicates()
            df.to_csv(output_file, index=False)
            print(f"数据已成功保存到 {output_file}")
        except Exception as e:
            print(f"保存 CSV 文件时出错: {e}")
    else:
        print("未找到有效的数据。")


def p3():
    import pandas as pd
    import json

    # 定义输入文件路径和输出文件路径
    input_file = r"D:\家宽\2025-04\压缩包2025-03-12T21_07_34.569(1)\file\staInfo_daily_rolling.log.202503121816.16.csv"
    output_file = r"D:\家宽\2025-04\压缩包2025-03-12T21_07_34.569(1)\file\staInfo_daily_rolling16.csv"

    # 用于存储所有处理后的数据
    all_data = []

    try:
        # 打开输入文件进行读取
        with open(input_file, 'r', encoding='utf-8') as file:
            # 逐行读取文件
            for line in file:
                try:
                    line = line[14:]
                    # 解析当前行的 JSON 数据
                    data = json.loads(line)
                    # 提取 MAC 和 timestamp
                    mac = data["MAC"]
                    timestamp = data["timestamp"]
                    # 检查 data 字段是否存在
                    res = data['data']['Devices']
                    del data['data']['Devices']
                    other = data['data']
                    other["MAC"] = mac
                    other["timestamp"] = timestamp
                    if len(res) == 0:
                        all_data.append(other)
                    else:
                        for t in res:
                            t.update(other)
                            # 将处理后的条目添加到 all_data 列表中
                            all_data.append(t)
                except json.JSONDecodeError:
                    print(f"无法解析行: {line}")
    except FileNotFoundError:
        print(f"错误: 文件 {input_file} 未找到。")
    except Exception as e:
        print(f"发生未知错误: {e}")

    # 如果 all_data 列表不为空
    if all_data:
        # 创建 DataFrame
        df = pd.DataFrame(all_data)
        try:
            # 将 DataFrame 保存为 CSV 文件
            print(df)
            # df = df.drop_duplicates()
            df.to_csv(output_file, index=False)
            print(f"数据已成功保存到 {output_file}")
        except Exception as e:
            print(f"保存 CSV 文件时出错: {e}")
    else:
        print("未找到有效的数据。")


def p4():
    import pandas as pd
    import json

    # 定义输入文件路径和输出文件路径
    input_file = r"D:\家宽\2025-04\压缩包2025-03-12T21_07_34.569(1)\file\wiFiStatus_daily_rolling.log.202503121816.15.csv"
    output_file = r"D:\家宽\2025-04\压缩包2025-03-12T21_07_34.569(1)\file\wiFiStatus_daily_rolling15.csv"

    # 用于存储所有处理后的数据
    all_data = []

    try:
        # 打开输入文件进行读取
        with open(input_file, 'r', encoding='utf-8') as file:
            # 逐行读取文件
            for line in file:
                try:
                    # 解析当前行的 JSON 数据
                    data = json.loads(line)
                    # 提取 MAC 和 timestamp
                    mac = data["MAC"]
                    timestamp = data["timestamp"]
                    other = {}
                    other["MAC"] = mac
                    other["timestamp"] = timestamp
                    # 检查 data 字段是否存在
                    if "data" in data and "Radios" in data["data"]:
                        res = data['data']['Radios']
                        for t in res:
                            t.update(other)
                            # 将处理后的条目添加到 all_data 列表中
                            all_data.append(t)
                    if "data" in data and "Configurations" in data["data"]:
                        res = data['data']['Configurations']
                        for t in res:
                            t.update(other)
                            # 将处理后的条目添加到 all_data 列表中
                            all_data.append(t)
                except json.JSONDecodeError:
                    print(f"无法解析行: {line}")
    except FileNotFoundError:
        print(f"错误: 文件 {input_file} 未找到。")
    except Exception as e:
        print(f"发生未知错误: {e}")

    # 如果 all_data 列表不为空
    if all_data:
        # 创建 DataFrame
        df = pd.DataFrame(all_data)
        try:
            # 将 DataFrame 保存为 CSV 文件
            print(df)
            # df = df.drop_duplicates()
            df.to_csv(output_file, index=False)
            print(f"数据已成功保存到 {output_file}")
        except Exception as e:
            print(f"保存 CSV 文件时出错: {e}")
    else:
        print("未找到有效的数据。")


if __name__ == '__main__':
    p4()
